package com.lune.job.controller;


import com.lune.job.framework.redis.RedisUtil;
import com.lune.job.framework.role.RequireRoles;
import com.lune.job.framework.role.Role;
import com.lune.job.pojo.Company;
import com.lune.job.pojo.ResPageBean;
import com.lune.job.pojo.RespBean;
import com.lune.job.service.ICompanyService;
import com.lune.job.utils.UserThreadLocal;
import com.lune.job.vo.UserData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/company")
@Api(tags = "公司管理")
public class CompanyController {

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation(value = "添加单位")
    @PostMapping("/")
    public RespBean create(@RequestBody Company company){
        boolean save = companyService.save(company);
        if (save){
            return RespBean.success("添加单位成功");
        }else{
            return RespBean.error("添加单位失败");
        }
    }

    @ApiOperation(value = "删除单位")
    @DeleteMapping("/")
    public RespBean delete(Integer[] ids){
        boolean b = companyService.removeByIds(Arrays.asList(ids));
        if (b){
            return RespBean.success("删除单位成功");
        }else{
            return RespBean.error("删除单位失败");
        }
    }

    @ApiOperation(value = "修改单位")
    @PutMapping("/")
    public RespBean update(@RequestBody Company company){
        boolean b = companyService.updateById(company);
        if (b){
            return RespBean.success("修改单位成功");
        }else{
            return RespBean.error("修改单位失败");
        }
    }

    @ApiOperation(value = "部分修改单位")
    @PutMapping("/updateBySel")
    public RespBean updateSelective(@RequestBody Company company){
        int flag = companyService.updateSelective(company);
        if (flag>0){
            return RespBean.success("修改单位成功");
        }else{
            return RespBean.error("修改单位失败");
        }
    }

    @ApiOperation(value = "根据id查询单位")
    @GetMapping("/detail")
    public RespBean detail(Integer id){
        Company company = companyService.getById(id);
      if (company!=null){
          return RespBean.success("根据id查询单位成功",company);
      }
        return RespBean.error("查询失败" );
    }

    @ApiOperation(value = "获取详细信息")
    @GetMapping("/info")
    @RequireRoles(type = Role.COMPANY)
    public RespBean info(){
        String token = UserThreadLocal.get();
        UserData userData = (UserData) redisUtil.get(token);
        Company byId = companyService.getById(userData.getId());
        if (byId!=null){
            return   RespBean.success("获取详细信息成功",byId);
        }
        return   RespBean.error("获取详细信息失败");
    }

    @ApiOperation(value = "分页查询单位")
    @GetMapping("/queryPage")
    public ResPageBean getAllStudentsByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                            @RequestParam(defaultValue = "10") Integer size,
                                            Company company) {
        return companyService.getAllCompanyByPage(currentPage, size, company);
    }

    @ApiOperation(value = "条件查询单位")
    @PostMapping("/queryByCon")
    public List<Company> queryByCon(@RequestBody Company company) {
        return companyService.queryByCon(company);
    }


}
